Ame Liu 22910358
1.pairs 矩阵散点图 探索两两变量之间的相关性
rw <-read.table('/Users/liuyu/Downloads/winequality-red.csv', head=T,sep=',',fileEncoding = 'UTF-8')
pairs(rw)
pairs(~pH+alcohol+quality+density, data=rw)
只显示下半边数据
pairs(~pH+alcohol+quality+density, data=rw,upper.panel = NULL)
绘制mpg图像
a <- data.frame(mpg)
pairs(~displ+cty+hwy, data=mpg)# mpg是ggplot2的内置数据集
绘制散点图,封装散点图(具体到每一个类),对比图
library(ggplot2)
summary(mpg$displ)#mpg的displ数据
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.600 2.400 3.300 3.472 4.600 7.000
ggplot(iris,aes(Sepal.Length,Sepal.Width))+geom_point()+facet_wrap(~Species)#作iris数据集的图
#针对iris数据库进行关于Species的分面处理
ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+facet_wrap(~class,ncol = 2)+labs(title = "point geom")#aes是设置横纵坐标,facet_wrap此处为封装型,geom_point()是绘制散点图,tile是设置标题.
ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+labs(title = "point geom")#点状图
ggplot(mpg,aes(displ,hwy))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")#平滑图
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
#通过gridExtra做对比图
p1 <- ggplot(mpg,aes(displ,hwy))+geom_point(colour='blue')+facet_wrap(~class)+labs(title = "point geom")
p2 <- ggplot(mpg,aes(displ,hwy))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")
library(gridExtra)
grid.arrange(p1, p2, ncol=2)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
展示一下mpg和iris内置数据集,是流行的车的数据
rrr <- data.frame(mpg)
rrr
mm <- data.frame(iris)
mm
Aesthetic levels
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class), show.legend = TRUE)
d<- ggplot(mpg,aes(x=displ,y=hwy,size = class))+geom_point(fill = "red",show.legend = TRUE)+labs(title = "point geom") #geom_point1
c <- ggplot(mpg,aes(x=displ,y=hwy,shape = drv,alpha = class))+geom_point(colour='black',fill = "red")+labs(title = "point geom") #geom_point2
a <- ggplot(mpg,aes(x=displ,y=hwy,group = drv))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")
#geom_smooth1
b <- ggplot(mpg,aes(x=displ,y=hwy,color = drv,linetype = drv))+geom_smooth(colour='yellow')+labs(title = "geom_smooth") #geom_smooth2
grid.arrange(a, b,c, d, ncol=2)
library(gridExtra)
p1 <- ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv)) #mapping意为映射
p2 <- ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, color = drv,
linetype = drv))
p3 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class),
show.legend = TRUE) #显示图例标注
p4 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class,
shape = drv))
grid.arrange(p1, p2, p3, p4, ncol=2)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning: Using size for a discrete variable is not advised.
Warning: Using alpha for a discrete variable is not advised.
ggplot(mpg,aes(x = displ, y = hwy))+geom_point()+geom_smooth()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
#作图(数据,坐标)+ 选择图形种类
ggplot(mpg,aes(x=displ,y=hwy,color=class))+geom_point()+geom_smooth()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
span too small. fewer data values than degrees of freedom.
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
pseudoinverse used at 5.6935
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
neighborhood radius 0.5065
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
reciprocal condition number 0
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
There are other near singularities as well. 0.65044
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
span too small. fewer data values than degrees of freedom.
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
pseudoinverse used at 5.6935
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
neighborhood radius 0.5065
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
reciprocal condition number 0
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
There are other near singularities as well. 0.65044
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
pseudoinverse used at 4.008
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
neighborhood radius 0.708
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
reciprocal condition number 0
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
There are other near singularities as well. 0.25
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
pseudoinverse used at 4.008
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
neighborhood radius 0.708
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
reciprocal condition number 0
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x else if (is.data.frame(newdata)) as.matrix(model.frame(delete.response(terms(object)), :
There are other near singularities as well. 0.25
Hexbin - 2D histogram geom_hex()为六边形散点图
library(hexbin)
a1 <- with(rw,{bin<-hexbin(x=pH,y=density,xbins=10) #xbins的意思为x即是个数,以10个位一组计数
plot(bin)})
ggplot(rw,aes(pH,density))+geom_smooth(colour='yellow')+labs(title = "geom_smooth")+geom_hex()
`geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
ggplot2之常见观察变量绘图geom https://www.jianshu.com/p/4e4f6168fa37
反映频率的图(适合整数):
ggplot(data = rw) +
geom_count(mapping = aes(x=pH, y=density))
近似热图
library(dplyr)
counting <- count(mpg, displ, hwy)
ggplot(counting,aes(displ,hwy)) +
geom_tile(mapping = aes(fill = n))#设n为数量